﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using beway.common;
using Beway.ValueAdded.Model;
using Beway.ValueAdded.Service;

namespace Beway.ValueAdded.Admin.Controllers
{
    public class BaseController : Controller
    {
        //
        // GET: /Base/
        AdminUserServ _admSvc = SingletonProvider<AdminUserServ>.Instance;

        public ActionResult GetValidateCode(int length)
        {
            var vCode = new ValidateCode();
            var code = vCode.CreateValidateCode(length);
            Session["ValidateCode"] = code;
            var bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/gif");
        }

        /// <summary>
        /// 获取session中的验证码的字符串，获取后清空session
        /// </summary>
        /// <returns></returns>
        protected string GetValidCodeInSession()
        {
            string ret = "";
            if (Session["ValidateCode"] != null)
            {
                ret = Session["ValidateCode"].ToString();
                Session["ValidateCode"] = null;
            }
            return ret;
        }

        /// <summary>
        /// 获取当前登录的管理员
        /// </summary>
        private AdminUser _CurrentAdmin;
        public AdminUser CurrentAdmin
        {
            get
            { 
                if (_CurrentAdmin == null && User.Identity.IsAuthenticated)
                {
                    _CurrentAdmin = _admSvc.GetByUserName(User.Identity.Name);
                }
                return _CurrentAdmin;
            }
        }


        /// <summary>
        /// 管理员当前角色 0: 系统管理员 1: 充值账户管理员
        /// </summary>
        private int _currentRole = -1;

        /// <summary>
        /// 当前登录管理员角色
        /// </summary>
        public ManagerRoleType CurrentRole
        {
            get
            {
                if (User.Identity.IsAuthenticated)
                {
                    if (Session["role"] != null)
                    {
                        
                        if (!int.TryParse(Session["role"].ToString(), out _currentRole))
                        {
                            _currentRole = -1;
                        }
                    }

                    if(_currentRole==-1)
                    {
                        var model = _admSvc.GetByUserName(User.Identity.Name);
                        if (model != null)
                        {
                            _currentRole = model.RoleID;
                            Session["role"] = _currentRole.ToString();
                        }
                    }
                }
                else
                {
                    Session["role"] = null;
                    _currentRole = -1;
                }
                return (ManagerRoleType)_currentRole;
            }
        }
    }

    public enum ManagerRoleType
    {
        /// <summary>
        /// 其它角色
        /// </summary>
        Other=-1, 

        /// <summary>
        /// 系统管理员
        /// </summary>
        SystemAdmin=0, 

        /// <summary>
        /// 项目管理员，只能看到所属的项目（可以对所属项目、充值明细进行管理，可以审核批量充值请求）
        /// </summary>
        MerchantAdmin=1,

        /// <summary>
        /// 批量充值管理员,属于项目管理员下的子集，可以查看项目充值明细、上传批量充值、查看充值批次、查看/导出批量充值明细
        /// </summary>
        BatchRechAdmin=2,       
    }
}
